package com.example.union;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * ClassName: StreamConnect
 * Package: com.example.union
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-22
 * Time: 23:16
 */

public class StreamConnect {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> sData = env.fromElements("hello", "data", "Scala");

        DataStreamSource<Integer> source = env.fromElements(1, 2, 3);

        //聚合算子 连接
        //实现分为两步
        //首先是 DataStream的数据源调用 connect 连接方法 返回的是ConnectedStream
        //然后ConnectedStream流调用 同处理方法 Map/flatMap/process
        //他们的参数和 类本质方法类似 知识添加了多一条流的处理过程 CoMap/CoProcess
        ConnectedStreams<String, Integer> connect = sData.connect(source);


        final SingleOutputStreamOperator<String> coMap = connect.map(new CoMapFunction<String, Integer, String>() {
            //coMap本质也是一个转换算子 拿不到宁一条流的数据
            //知识有两个map方法 各自流 处理各自的方法

            @Override
            public String map1(String value) throws Exception {
                return "字符流: " + value;
            }

            @Override
            public String map2(Integer value) throws Exception {
                return "数字流: " + value;
            }
        });

        coMap.print();

        env.execute();
    }
}
